/*
* Copyright (c) 2007 by Kirill Kolodyazhniy.
* See the file "license.terms" for information on usage and redistribution.
*/

#include "camera.h"
#include "rendertarget.h"
#include "exception.h"

namespace Engine
{
	/**************************************************************************************************************/
	RenderTarget::RenderTarget()
		:m_priority(0)
		,m_fps(0)
		,m_frameCount(0)
		,m_lastTime(0)
		,m_lastSecond(0)
		,m_triangleCount(0)
		,m_width(0)
		,m_height(0)
		,m_backGroundColor(Color::Black)
	{
	}
	/**************************************************************************************************************/
	void RenderTarget::Update(void)
	{
		if(!m_camera.IsNull())
		{

			m_camera->RenderScene(this);

			m_triangleCount = m_camera->GetNumRenderedFaces();

			UpdateFPS();
		}
	}
	/**************************************************************************************************************/
	void RenderTarget::UpdateFPS()
	{
		++m_frameCount;
        unsigned long thisTime = m_timer.GetMilliseconds();

        m_lastTime = thisTime ;

        if (thisTime - m_lastSecond > 1000)
        {
            m_fps = static_cast<double>(m_frameCount) / static_cast<double>(thisTime - m_lastSecond) * 1000.0;

            m_lastSecond = thisTime ;
            m_frameCount  = 0;
        }
	}
	/**************************************************************************************************************/
	double RenderTarget::GetFPS()
	{
		return m_fps;
	}
	/**************************************************************************************************************/
	unsigned int RenderTarget::GetTriangleCount()
	{
		return m_triangleCount;
	}
	/**************************************************************************************************************/
	void RenderTarget::SetCamera(CameraPtr camera)
	{
		m_camera = camera;
	}
	/**************************************************************************************************************/
	void RenderTarget::SetBackGroundColor(const Color& color)
	{
		m_backGroundColor = color;
	}
	/**************************************************************************************************************/
	const Color& RenderTarget::GetBackGroundColor()const
	{
		return m_backGroundColor;
	}
	/**************************************************************************************************************/
}

